from django.shortcuts import render
import jieba
from pypinyin import Style, pinyin, load_phrases_dict
from disn.utilities import *
# Create your views here.

load_phrases_dict({'步履蹒跚': [['bù'], ['lǚ'], ['pán'], ['shān']]})
load_phrases_dict({'妈妈': [['mā'], ['ma']]})

def display_pinyin(request):
    if request.method == 'GET':
        return render(request, 'article.html')
    else:
        content = request.POST.get('content', '')
        # print(content)
    param_list = []
    
    # r = re.compile(r"^(.+)(?:\n|\r\n?)((?:(?:\n|\r\n?).+)+)", re.MULTILINE) #   re.DOTALL
    # lines = r.findall(content)
    # hanzi = re.compile(ur'[\u4E00-\u9FA5]+')
    lines = content.splitlines()
    print('toal lines=',len(lines))
    if len(lines) > 0:
        for line in lines:
            seg_list = jieba.cut(line)
            fenci_list = [x for x in seg_list]
            word_list = []
            sentence = ''
            for cizu in fenci_list:
                cizu_py = pinyin(cizu)
                sentence += cizu + '|'
                if judge_pure_english(cizu):
                    word_list.append((cizu, cizu_py[0][0]))
                    # sentence += ' / ' + cizu + '(' + cizu_py[0][0] +')'
                else:
                    for index,word in enumerate(cizu):
                        word_list.append((word, cizu_py[index][0]))
                    # sentence += ' / ' + cizu + '(' + str(cizu_py) +')'
            print(sentence)
            param_list.append(word_list)
            # for word in line:
            #     if re.match(hanzi, word):
            #         py = pinyin.get_pinyin(word, show_tone_marks=True)
            #     else:
            #         py = '&nbsp;'
            #     word_list.append((word, py))
            
    # else:
    #     word_list = []
    #     for word in content:
    #         word_list.append((word, pinyin.get_pinyin(word, show_tone_marks=True)))
    #     param_list.append(word_list)
    print('length of param_list:', len(param_list))
    return render(request, 'article.html', {'param_list':param_list, 'content':content})